package com.hui.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hui.config.Result;
import com.hui.entity.User;
import com.hui.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @BelongsProject: bilibili-qingGe
 * @BelongsPackage: com.hui.service
 * @Author: HUI
 * @CreateTime: 2021-12-02 14:40
 * @Description:
 */

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    //新增用户
    public int saveUser(User user) {
        return userMapper.insert(user);
    }


    //分页模糊查询
    public Page<User> queryUser(Integer pageNum,
                                Integer pageSize,
                                String searchStr) {

        Page<User> userPage = new Page<>(pageNum, pageSize);


        //lambda 模糊查询：user 表中名字相似于 search 的

        LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
        if (!searchStr.isBlank()) {
            wrapper.like(User::getNickName, searchStr);
        }


        return userMapper.selectPage(userPage, wrapper);


    }


    //修改用户
    public int updateUser(@RequestBody User user) {
        return userMapper.updateById(user);
    }


    //删除用户
    public int deleteUser(Integer id) {
        return userMapper.deleteById(id);
    }


    //登录查询
    public User loginUser(User user) {

        LambdaQueryWrapper<User> data = Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()).eq(User::getPassword, user.getPassword());

        return userMapper.selectOne(data);
    }


    //按 用户名 查询（新增用户时，查是否有重复）
    public User addQuery(User user) {

        LambdaQueryWrapper<User> data = Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername());

        return userMapper.selectOne(data);
    }


    //查询当前登录的用户信息
    public User queryOwn(Integer id) {
        return userMapper.selectById(id);

    }
}
